 <template>
   <view class="container">
     <!-- 浮动按钮 -->
     <view class="floating-button" @click="onFloatingButtonClick">
       <image src="/static/images/icon/toadd.png" class="icon-img" mode="aspectFit" />
     </view>
 
     <!-- 查询条件 -->
     <view class="filters">
       <view @click="toggleFilters" class="filter-header">
         <text>查询条件</text>
         <text>{{ showFilters ? '收起' : '展开' }}</text>
       </view>
       <view v-if="showFilters" class="filter-body">
         <input class="filter-input" v-model="query.no" placeholder="请输入采购单编号" />
         <input class="filter-input" v-model="query.supplierId" placeholder="请输入供应商编号" />
         <input class="filter-input" v-model="query.remark" placeholder="请输入备注" />
         <view class="item-actions-inline">
           <button @click="search()" class="action-button">查询</button>
           <button @click="reset()" class="action-button danger">重置</button>
         </view>
       </view>
     </view>
 
     <!-- 列表 -->
     <view class="list-container">
       <view v-for="item in listData" :key="item.id" class="list-item">
         <view @click="viewDetails(item)">
           <view class="list-detail">编号：{{ item.no }}</view>
           <view class="list-detail">供应商：{{ item.supplierName }}</view>
           <view class="list-detail">产品：{{ item.productNames }}</view>
           <view class="list-detail">采购时间：{{ item.orderTime }}</view>
           <view class="list-detail">总数量：{{ item.totalCount }}</view>
           <view class="list-detail">金额：{{ item.totalPrice }}</view>
           <view class="list-detail">退货数：{{ item.returnCount }}</view>
           <view class="list-detail">状态：{{ formatStatus(item.status) }}</view>
           <view class="divider"></view>
         </view>
         <view class="item-actions-inline">
			 <button @click="updateStatus(item.id, '20')" class="action-button">审核</button>
           <button @click="updateStatus(item.id, '10')" class="action-button">反审核</button>
           <button @click="updateStatus(item.id, '10')" class="action-button">出库</button>
           <button @click="editItem(item)" class="action-button">修改</button>
           <button @click="deleteItem(item)" class="action-button danger">删除</button>
         </view>
       </view>
       <view v-if="!hasMoreData" class="no-more-data">没有更多数据了</view>
       <view v-if="loading" class="loading-text">加载中...</view>
     </view>
   </view>
 </template>
 
 <script>
 import { pageList, deleteOrders, updateSaleOrderStatus } from "@/sheep/api/erp/purchase.js";
 
 export default {
   data() {
     return {
       showFilters: false,
       query: {
         no: "",
         supplierId: "",
         remark: "",
       },
       listData: [],
       pageNum: 1,
       pageSize: 10,
       loading: false,
       hasMoreData: true,
     };
   },
   methods: {
     // 查询条件切换
     toggleFilters() {
       this.showFilters = !this.showFilters;
     },
     reset() {
       this.query.no = "";
       this.query.supplierId = "";
       this.query.remark = "";
       this.refreshData();
     },
     // 查询数据
     search() {
       this.refreshData();
     },
     // 刷新数据
     refreshData() {
       this.pageNum = 1;
       this.hasMoreData = true;
       this.listData = [];
       this.loadMoreData();
     },
     // 加载更多数据
     loadMoreData() {
       if (this.loading || !this.hasMoreData) return;
 
       this.loading = true;
       const params = {
         pageNo: this.pageNum,
         pageSize: this.pageSize,
         ...this.query,
       };
       pageList(params)
         .then((res) => {
           const { list, total } = res.data;
           if (this.pageNum * this.pageSize >= total) {
             this.hasMoreData = false;
           }
           this.listData = [...this.listData, ...list];
         })
         .catch((error) => {
           console.error("加载数据失败:", error);
         })
         .finally(() => {
           this.loading = false;
           uni.stopPullDownRefresh();
         });
     },
     onPullDownRefresh() {
       this.refreshData();
     },
     onReachBottom() {
       if (!this.hasMoreData) return;
       this.pageNum++;
       this.loadMoreData();
     },
     // 浮动按钮点击
     onFloatingButtonClick() {
       uni.setStorageSync("orderId", "");
       uni.navigateTo({
         url: "/pages/erp/purchase/edit",
       });
     },
     async updateStatus(id, status) {
       try {
         await updateSaleOrderStatus(id, status);
         this.showToast("提交成功", "success");
       } catch (error) {
         console.log("error", error);
         this.showToast("提交失败:" + error.msg);
       } finally {
         this.refreshData();
       }
     },
     // 详情、修改、删除
     viewDetails(item) {
       const orderId = encodeURIComponent(item.id);
       uni.setStorageSync("orderId", orderId);
       console.log("详情:", orderId);
       uni.navigateTo({
         url: "/pages/erp/purchase/show",
       });
     },
     editItem(item) {
       const orderId = encodeURIComponent(item.id);
       uni.setStorageSync("orderId", orderId);
       console.log("修改任务:", orderId);
       uni.navigateTo({
         url: "/pages/erp/purchase/edit",
       });
     },
     async deleteItem(item) {
       console.log("删除任务:", item);
       try {
         if (item.id) {
           await deleteOrders(item.id);
         }
         this.refreshData();
       } catch (error) {
         console.error("订单删除失败:", error);
       }
     },
     formatStatus(status) {
       const statusMap = {
         1: "待审核",
         2: "已审核",
         3: "已完成",
       };
       return statusMap[status] || "未知状态";
     },
   },
   onLoad() {
     uni.setStorageSync("orderId", "");
     this.refreshData();
   },
   onShow() {
     uni.setStorageSync("orderId", "");
     this.refreshData(); // 页面每次显示时刷新数据
   },
 };
 </script>

<style scoped>
 
</style>